<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8"/>
    <title>我的家</title>
    <link href="${pageContext.request.contextPath}/static/images/houserent.ico" rel="icon">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/module/admin.css?v=318" media="all"/>
</head>
<body onscroll="layui.admin.hideFixedEl();" class="theme-pink close-footer">
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form toolbar table-tool-mini">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索:</label>
                        <div class="layui-input-block">
                            <select name="houseStatus" lay-verType="tips" lay-verify="required" required>
                                <option value="">请选择状态</option>
                                <option value="3">已预订</option>
                                <option value="4">已租赁</option>
                                <option value="5">已退租</option>
                                <option value="6">申请退租</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" lay-filter="searchTenantHome" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <table id="tenantHome" lay-filter="tenantHome"></table>
        </div>
    </div>
</div>

<script type="text/html" id="tenantHomeEdit">
    {{# if(d.houseStatus == 3 | d.houseStatus == 6){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="cancel">取消</a>
    {{# } }}
    {{# if(d.houseStatus == 4){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="surrender">退租</a>
    {{# } }}
    {{# if(d.houseStatus == 5 | d.houseStatus == 7 |  d.houseStatus == 8){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{# } }}
</script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/libs/layui/layui.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/common.js?v=318"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#tenantHome',
            url: '${pageContext.request.contextPath}/house/tenanthome',
            page: true,
            cellMinWidth: 100,
            cols: [[
                // {field: 'id', title: "id",align: 'center',sort: true},
                {type: 'numbers'},
                {field: 'tenantName', title: '房客', align: 'center',minWidth: 50},
                {field: 'landlordName', title: '房东', align: 'center',minWidth: 50},
                {field: 'landlordPhone', title: '房东电话', align: 'center',minWidth: 150},
                {field: 'houseName', title: '房名', align: 'center',minWidth: 150},
                {field: 'houseAddress', title: '房屋地址', align: 'center',minWidth: 150},
                {
                    field: 'houseStatus', title: '状态', templet: function (d) {
                        var strs = [
                            '<span class="text-success">已预订</span>',
                            '<span class="text-success">已租赁</span>',
                            '<span class="text-danger">已退租</span>',
                            '<span class="text-danger">申请退租</span>',
                            '<span class="text-danger">已下线</span>',
                            '<span class="text-danger">拒绝</span>'
                        ];
                        return strs[d.houseStatus-3];
                    }, align: 'center',minWidth: 70
                },
                {field: 'housePrice', title: '租金(月)', align: 'center'},
                {field: 'rentTime', title: '租期(月)', align: 'center'},
                {
                    field: 'houseType', title: '租赁类型', templet: function (d) {
                        var strs = [
                            '<span class="text-success">整租</span>',
                            '<span class="text-success">合租</span>',
                            '<span class="text-danger">已下线</span>'
                        ];
                        return strs[d.houseType-8];
                    }, align: 'center',minWidth: 50
                },
                {title: '操作', toolbar: '#tenantHomeEdit', align: 'center', minWidth: 100}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(searchTenantHome)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(tenantHome)', function (obj) {
            var data = obj.data; // 获得当前行数据
            if (obj.event === 'cancel') { // 取消
                layui.admin.confirm('确认取消吗？', function (index) {
                    $.ajax({
                        url:'${pageContext.request.contextPath}/order/canceltenant',
                        data: data,
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            if(res.code == 0){
                                layer.msg(res.msg,{icon:6},function () {
                                    window.location.reload();
                                })
                            }else{
                                layer.msg(res.msg,{icon:5});
                            }
                        },
                        error:function () {
                            layer.msg('请求失败');
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'surrender') { // 退租
                layui.admin.confirm('确认退租吗？', function (index) {
                    $.ajax({
                        url:'${pageContext.request.contextPath}/house/surrendertenant',
                        data: data,
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            if(res.code == 0){
                                layer.msg(res.msg,{icon:6},function () {
                                    window.location.reload();
                                })
                            }else{
                                layer.msg(res.msg,{icon:5});
                            }
                        },
                        error:function () {
                            layer.msg('请求失败');
                        }
                    })
                    layer.close(index);
                });
            }else if (obj.event === 'del') { // 删除
                layui.admin.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    $.ajax({
                        url:'${pageContext.request.contextPath}/house/deltenhome',
                        data: data,
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            if(res.code == 0){
                                layer.msg(res.msg,{icon:6},function () {
                                    window.location.reload();
                                })
                            }else{
                                layer.msg(res.msg,{icon:5});
                            }
                        },
                        error:function () {
                            layer.msg('请求失败');
                        }
                    })
                });
            }
        });
    });
</script>
</body>
</html>
